package com.example.androidstart.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Outline;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewOutlineProvider;

import androidx.annotation.Nullable;

public class BiggerView extends View {


    private Bitmap mBitmap;

    private float mDownX;
    private float mDownY;

    private static final String TAG = "BiggerView";

    public BiggerView(Context context) {
        super(context);
        init();
    }

    public BiggerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BiggerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public BiggerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private Paint mPaint;

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(5);
//        BiggerView.this.setOutlineProvider(new ViewOutlineProvider() {
//            @Override
//            public void getOutline(View view, Outline outline) {
//                outline.setOval(0, 0, view.getWidth(), view.getHeight());
//            }
//        });
//        BiggerView.this.setClipToOutline(true);


    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.save();

        int width = getWidth();
        int height = getHeight();
        canvas.clipRect(0, 0, width, height);
        float scaleFactor = 2f;
        canvas.scale(scaleFactor, scaleFactor);
//        canvas.scale(1f, 1f);


        // 为了让触摸点在中心，加上view的一半
        float xDelta = -(mDownX) + (width * 1.0f / 2 / scaleFactor);
        float yDelta = -mDownY + height * 1.0f / 2 / scaleFactor;

        Log.i(TAG, "onDraw: mDownX mDownY " + mDownY + " mDOwnX" + mDownX);

        if (mBitmap != null) {
            canvas.drawBitmap(mBitmap, xDelta, yDelta, mPaint);
        } else {
            Log.i(TAG, "onDraw: mBitmap is null");
        }
        canvas.restore();

        super.onDraw(canvas);
    }

    public void downXY(float x, float y) {
        mDownX = x;
        mDownY = y;
        Log.i(TAG, "onTouch: ACTION_MOVE x,y" + x + "," + y);
    }

    public void setBitmap(Bitmap mBitmap) {
        this.mBitmap = mBitmap;
    }
}
